package com.wbxqy.car_management_system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wbxqy.car_management_system.entity.Department;
import com.wbxqy.car_management_system.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 部门表 前端控制器
 * </p>
 *
 * @author wbxqy
 * @since 2024-07-04
 */
@RestController
@RequestMapping("/wbxqy/departments")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    @PostMapping
    public boolean addDepartment(@RequestBody Department department) {
        return departmentService.save(department);
    }

    @DeleteMapping("/{id}")
    public boolean deleteDepartment(@PathVariable Integer id) {
        return departmentService.removeById(id);
    }

    @PutMapping
    public boolean updateDepartment(@RequestBody Department department) {
        return departmentService.updateById(department);
    }

    @GetMapping("/{id}")
    public Department getDepartmentById(@PathVariable Integer id) {
        return departmentService.getById(id);
    }

    @GetMapping("/page")
    public Page<Department> getAllDepartmentsPage(@RequestParam(required = false, defaultValue = "1") Integer page,
                                                     @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                                     @RequestParam(required = false) String departmentName,
                                                     @RequestParam(required = false) String departmentDesc) {
        // 构造分页构造器
        final Page<Department> pageInfo = new Page<>(page, pageSize);
        final QueryWrapper<Department> queryWrapper = new QueryWrapper<>();

        // 添加过滤条件
        if (departmentName != null && !departmentName.isEmpty()) {
            queryWrapper.like("department_name_wbxqy", departmentName);
        }
        if (departmentDesc != null && !departmentDesc.isEmpty()) {
            queryWrapper.like("department_desc_wbxqy", departmentDesc);
        }

        // 执行分页查询
        departmentService.page(pageInfo, queryWrapper);

        return pageInfo;
    }

    @GetMapping("")
    public List<Department> listDepartment(){
        return departmentService.list();
    }
}

